<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


/* *
 * 类名：Business
 * 功能：商家后台、管理商务插件
 * 详细：发布团购、管理团购、管理订单
 * 版本：1.0
 * 日期：2011-07-04
 * 说明：
 * 本插件由易站网络（www.yzweb.co.cc）开发。
 * 该代码仅供购买者使用

 *************************注意*************************
 * 
 */
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/app.php');

need_manager();
need_auth('admin|market');

$id = abs(intval($_GET['id']));
$team = Table::Fetch('team', $id);

if ( $team['delivery']=='express' ) {
	$oc = array(
		'state' => 'pay',
		'team_id' => $id,
	);
	$orders = DB::LimitQuery('order', array(
		'condition' => $oc,
		'order' => 'ORDER BY pay_time DESC, id DESC',
	));
	$kn = array(
		'team_id' => '项目ID',
		'id' => '订单ID',
		'buy_id' => '支付序号',
		'express_id' => '快递ID',
		'express_name' => '快递名称',
		'express_no' => '快递单号',
		'state' => '支付状态',
		'realname' => '收件人',
		'mobile' => '联系电话',
		'zipcode' => '邮政编码',
		'address' => '收货地址',
		'quantity' => '商品数量',
		'condbuy' => '商品选项',
		'price' => '商品单价',
		/*'money' => '支付款',
		'credit' => '余额付款',
		'service' => '支付方式',*/
		'fare' => '运费',
		'origin' => '商品总金额',
		'remark' => '客户备注',
		/*'username' => '用户名',
		'email' => '用户邮箱',
		'usermobile' => '用户手机',*/
		'date' => '支付时间',
	);
	
	$state = array(
		'unpay' => '未支付',
		'pay' => '已支付',
		);
	
	foreach($orders As $k=>$o) {
		$category = Table::Fetch('category', $o['express_id']);
		$o['team_id'] = $team['id'];
		$o['express_name'] = $category['name'];
		$o['state'] = $state[$o['state']];
		//$o['kefu_remarks'] = $team['kefu_remarks'];
		$o['date'] = date('Y-m-d H:i', $o['pay_time']);
		$orders[$k] = $o;
	}

	$name = "team_{$id}_".date('Ymd');
	down_xls($orders, $kn, $name);
}
else {
	$cc = array(
		'team_id' => $id,
		);
	$coupons = DB::LimitQuery('coupon', array(
				'condition' => $cc,
				'order' => 'ORDER BY create_time ASC',
				));
	$users = Table::Fetch('user', Utility::GetColumn($coupons, 'user_id'));
	$orders = Table::Fetch('order', Utility::GetColumn($coupons, 'order_id'));
	$kn = array(
			'buy_id' => '支付序号',
			'username' => '用户名',
			'email' => '用户邮箱',
			'realname' => '姓名',
			'mobile' => '手机号码',
			'condbuy' => '选项',
			'id' => "{$INI['system']['couponname']}编号",
			'secret' => "{$INI['system']['couponname']}密码",
			'cmobile' => '消费手机',
			'date' => '生成时间',
			'remark' => '备注',
	);
	
	foreach($coupons As $k=>$o) {
		$u = $users[$o['user_id']];
		$r = $orders[$o['order_id']];

		$o['buy_id'] = $r['buy_id'];
		$o['username'] = $u['username'];
		$o['realname'] = $u['realname'];
		$o['condbuy'] = $r['condbuy'];
		$o['mobile'] = $u['mobile'];
		$o['email'] = $u['email'];
		$o['cmobile'] = $r['mobile'] ? $r['mobile'] : $u['mobile'];
		$o['date'] = date('Y-m-d H:i', $o['create_time']);
		$o['remark'] = $r['remark'];
		$coupons[$k] = $o;
	}
	$name = "team_{$id}_".date('Ymd');
	down_xls($coupons, $kn, $name);
}
